<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color: red;
    }
  </style>
</head>
<body>
  <div id="app">
    <!-- 视图定义 router-view组件 路由组件的出口 匹配路由组件就会在router-view位置处渲染 -->
    <button @click="enterHome">去首页</button>
    <router-link to="/news">到新闻页</router-link>
    <button @click="enterAbout">去关于我们</button>
    <button @click="addNewRoute">动态添加一个路由</button>
    <router-link to="/join">跳转到新增的路由</router-link>
    <button @click="$router.go(0)">刷新</button>
    <hr>
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>

  <script>
    // 先准备  三个 路由组件  config
    const Home = {
      template: `
        <div>
          <h2>这是首页</h2>  
          <router-view></router-view>
        </div>
      `,
      mounted(){
        console.log(this.$router, 111);
      }
    }
    const News = {
      template: `
        <div>
          <h2>这是新闻页</h2>  
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>这是关于我们页</h2>  
        </div>
      `
    }
    const NotFound = {
      template: `
        <div>
          <h2>404啦</h2>  
        </div>
      `
    }
    const JoinUs = {
      template: `
        <div>
          <h2>人才招聘</h2>  
        </div>
      `
    }
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home', // 路由path
        name: 'home',
        component: Home
      },
      {
        path: '/news',
        name: 'news',
        component: News
      },
      {
        path: '/about',
        name: 'about',
        component: About
      },
      /* 
        vue提供了一个特殊的path * 可以匹配任意路由地址 优先级是最低的
      */
      {
        path: '*',
        component: NotFound
      }
    ]
    // 定义路由实例对象并 注册 路由规则
    const router = new VueRouter({
      // routes 路由规则
      routes
    });
    console.log(router, 222);
    // 将路由实例 挂载到vm上
    const vm = new Vue({
      el: '#app',
      methods: {
        enterHome(){
          // this.$router.push('/home')
          /* this.$router.push({
            name: 'home'
          }) */
          this.$router.push({
            path: '/home'
          })
        },
        enterAbout(){
          this.$router.replace('/about')
        },
        addNewRoute(){
          /* this.$router.addRoute({
            path: '/join',
            name: 'joinUs',
            component: JoinUs
          }); */
          this.$router.addRoute(
            'home'
            ,
            {
              path: '/join',
              name: 'joinUs',
              component: JoinUs
            }
          );
          console.log(this.$router.getRoutes(),111);
        }
      },
      router
    })
  </script>
</body>
</html>